package com.bsj.media.queue;

import com.alibaba.fastjson.JSONObject;
import com.bsj.consumerQ.function.HandleRunT;
import com.bsj.media.init.websocket.SessionChannelUntil;
import com.bsj.media.init.websocket.UserIdSessionChannelUntil;
import com.bsj.power.common.config.global.JsonResultExpend;
import com.bsj.springQueue.runQueue.UseInterface;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.yeauty.pojo.Session;

import java.util.List;
import java.util.Set;

@Component
@Slf4j
public class PushMsgQueue extends UseInterface<JsonResultExpend> {

    private int ALARM = 1;

    private int MEDIA = 2;


    @Override
    public HandleRunT<JsonResultExpend> gainHandleRun() {
        return new HandleRunT<JsonResultExpend>() {
            @Override
            public void doRun(List<JsonResultExpend> list) {
                for (JsonResultExpend jsonResultExpend : list) {
                    int type = jsonResultExpend.getType();
                    if (type == MEDIA) {
                        String id = jsonResultExpend.getId();
                        Set<Session> sessions = SessionChannelUntil.getChannelAndIp(id);
                        if (sessions != null && !sessions.isEmpty()) {
                            sessions.stream().forEach(session -> {
                                session.sendText(JSONObject.toJSONString(jsonResultExpend));
                            });
                        }
                    } else if (type == ALARM) {
                        List<Long> userList = jsonResultExpend.getUserList();
                        log.info("获得用户数据:" + JSONObject.toJSONString(userList));
                        if (userList != null && !userList.isEmpty()) {
                            for (Long userId : userList) {
                                Set<Session> gain = UserIdSessionChannelUntil.gain(userId);
                                log.info("开始推送数据:" + JSONObject.toJSONString(jsonResultExpend));
                                if (gain != null && !gain.isEmpty()) {
                                    gain.stream().forEach(session -> {
                                        session.sendText(JSONObject.toJSONString(jsonResultExpend));
                                    });
                                }
                            }
                        }
                    }
                }
            }
        };
    }

}
